package com.ctg.payroll2.service.sysadmin.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ctg.payroll2.bean.po.PbSecgroup;
import com.ctg.payroll2.service.sysadmin.ResourceManService;
import com.ctg.platform.core.security.SecurityUitls;
import com.ctg.platform.core.service.BaseBzServiceImpl;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;

public class ResourceManServiceImpl extends BaseBzServiceImpl implements ResourceManService{
	private IbatisEntityDao ibatisEntityDao;

	public void addNode(PbSecgroup node) {
		node.setGroupCode(sysUtilManager.getGUID());
		ibatisEntityDao.getSqlMapClientTemplate().insert("res.addNode", node);
	}

	public void deleteNodeByNodeId(String nodeId) {
		ibatisEntityDao.getSqlMapClientTemplate().delete("res.deleteNodeByNodeId", nodeId);
		ibatisEntityDao.getSqlMapClientTemplate().delete("res.deleteSeckeyByGroupId", nodeId);
	}
	
	public int getChildrenCountByParentId(String id){
		return (Integer)ibatisEntityDao.getSqlMapClientTemplate().queryForObject("res.getChildrenByParentId_count", id);
	}

	public void updateNode(PbSecgroup node) {
		ibatisEntityDao.getSqlMapClientTemplate().update("res.updateNode", node);
	}
	
	public PbSecgroup retrieveNode(String nodeId){
		List list = ibatisEntityDao.getSqlMapClientTemplate().queryForList("res.retrieveNode", nodeId);
		if(list.size()>1)
			return null;
		else 
			return (PbSecgroup)list.get(0);
	}
	
	public void deleteSeckeyByResIds(String[] ids){
		this.ibatisEntityDao.getSqlMapClientTemplate().delete("res.deleteSeckeyByResIds", SecurityUitls.parseIntarraytoStr(ids, ","));
	}
	
	public void adResId2PbSeckey(String resId, String pbGroupCode){
		if (StringUtils.isEmpty(resId)||StringUtils.isEmpty(pbGroupCode)) {
			return;
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("resId", resId);
		map.put("pbGroupCode", pbGroupCode);
		ibatisEntityDao.getSqlMapClientTemplate().insert("res.adResId2PbSeckey", map);
	}
	
	public String getNodeByNodeName(String name){
		List list = ibatisEntityDao.getSqlMapClientTemplate().queryForList("res.getNodeByNodeName", name);
		
		return list.size()==1?((PbSecgroup)list.get(0)).getGroupCode():null;
	}

	/**
	 * @param ibatisEntityDao 设置ibatisEntityDao
	 */
	public void setIbatisEntityDao(IbatisEntityDao ibatisEntityDao) {
		this.ibatisEntityDao = ibatisEntityDao;
	}
}
